package com.example.controller;

import com.example.entity.SecKillOrder;
import com.example.entity.SecKillSku;
import com.example.entity.SecKillSpu;
import com.example.entity.SecKillUser;
import com.example.service.OrderService;
import com.example.service.SkuService;
import com.example.service.SpuService;
import com.example.service.UserService;
import jakarta.annotation.Resource;
import java.util.List;
import java.util.Objects;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * (CommodityOrder)表控制层
 *
 * @author 游家纨绔
 * @since 2023-09-05 23:30:00
 */
@Controller
@RequestMapping("order")
public class OrderController {

	/**
	 * 服务对象
	 */
	@Resource
	private OrderService orderService;
	@Resource
	private SpuService spuService;
	@Resource
	private SkuService skuService;
	@Resource
	private UserService userService;


	@GetMapping("goOrder")
	public String goOrder(Integer userId, Model model) {
		// 获取用户信息
		SecKillUser secKillUser = userService.selectByUserId(userId);
		// 获取用户订单（返回树结构）
		List<SecKillOrder> secKillOrders = orderService.selectByUserId(userId);
		secKillOrders.forEach(order -> {
			SecKillSpu secKillSpu = spuService.selectById(order.getSpuId());
			SecKillSku secKillSku = skuService.selectById(order.getSkuId());
			if (Objects.equals(secKillSku.getSpuId(), order.getSpuId())) {
				secKillSpu.getSkuList().add(secKillSku);
			}
			order.getSpuList().add(secKillSpu);
		});
		// 包装模版
		model.addAttribute("orders", secKillOrders);
		model.addAttribute("user", secKillUser);
		return "order-show";
	}

}
